/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataaccess;

import abstractmodel.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author littlechild
 */
public class DataSource implements IDataSource {
    private final static String loginfile="data"+File.separator+"login.dat";
    private final static String keyForFileName="用户名";
    private JDOmanager jdomanager=new JDOmanager();
    public DataSource() {
    }

    public IInfo loadDetailInfo(int id) {
        ArrayList<IInfo> infos=search("id=="+id, null);
        if(infos.size()==0)
            return null;
        return infos.get(0);
    }

    public boolean saveDetailInfo(IInfo info) {
        return jdomanager.saveDetailInfo(info);
    }

    public boolean deleteDetailInfo(IInfo info) {
        return jdomanager.deleteDetailInfo(info);
    }

    public ArrayList<IInfo> search(String filter, Class info) {
        return jdomanager.search(filter,info);
    }
    public void close() {
        jdomanager.close();
    }

    public boolean saveUser(IUser user) {
        ObjectOutputStream oout=null;
        try {
            String filename = user.getUserInfo().getValue(keyForFileName).getString()+".dat";
            oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
            oout.writeObject(user);
            oout.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return false;
    }

    public IUser loadUser(String name) {
        ObjectInputStream in=null;
        try {
            String filename = name + ".dat";
            in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename)));
            IUser user=(IUser)in.readObject();
            in.close();
            return user;
        } catch (IOException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return null;
    }

    public boolean deleteUser(IUser user) {
        String filename=user.getUserInfo().getValue(keyForFileName)+".dat";
        for(IBook book:user.getBooks()) {
            for(IAccount account:book.getAccounts()) {
                ArrayList<IDetail> details =account.searchDetailInfo("id>=0");
                for(IDetail detail:details) {
                    this.deleteDetailInfo(detail.getDetailInfo());
                }
            }
        }
        this.loadLogInInfo().remove(user.getUserInfo().getValue("用户名").getString());
        File f=new File(filename);
        if(f.exists()) {
            return f.delete();
        }
        return true;
    }

    public Map loadLogInInfo() {
        ObjectInputStream oin=null;
        try {
            File f = new File(loginfile);
            oin = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
            return (Map)oin.readObject();
        } catch (IOException ex) {
            System.out.println("abc");
            //Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        } 
        if(oin!=null) {
            try {
                oin.close();
            } catch (IOException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return new TreeMap();
    }

    public boolean saveLogInInfo(Map map) {
        try {
            ObjectOutputStream oout = null;
            oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(loginfile)));
            oout.writeObject(map);
            oout.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
